<?php

namespace app\api\model\admin\setting;

use app\api\common\model\BaseModel;
use app\api\common\traits\ModelTrait;

/**
 * 管理员权限规则
 * Class Permission
 *
 * @package app\model\admin
 */
class Permission extends BaseModel
{
    use ModelTrait;

    protected $connection = 'mysql';

    /**
     * 数据表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     *
     * @var string
     */
    protected $name = 'admin_permission';

    //软删除，查询时会自动加上 xxx IS NULL
    use \think\model\concern\SoftDelete;

    protected string $deleteTime = 'delete_time';

    /**
     * 删除当前菜单第子级菜单
     *
     * @param $menuId
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function deleteSubMenu($menuId)
    {
        $subMenus = $this->where('pid', $menuId)->select();
        foreach ($subMenus as $subMenu) {
            $this->deleteSubMenu($subMenu['id']);
            $subMenu->delete();
        }
    }

    public function searchIdInAttr($query, $value, $data)
    {
        if ($value) {
            $query->whereIn('id', $value);
        }
    }
}
